#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/un.h>

#define SOCK_FILE "/tmp/sock"

int main()
{
	//创建socket
	int sockfd = socket(AF_LOCAL,SOCK_DGRAM,0);
	if(0 > sockfd)
	{
		perror("socket");
		return -1;
	}

	//准备地址
	struct sockaddr_un addr = {};
	addr.sun_family = AF_LOCAL;
	strcpy(addr.sun_path,SOCK_FILE);

	//绑定
	if(connect(sockfd,(struct sockaddr*)&addr,sizeof(addr)))
	{
		perror("bind");
		return -1;
	}

	char buf[1024] = {};
	for(;;)
	{
		printf(">");
		gets(buf);
		int ret = write(sockfd,buf,strlen(buf)+1);
		if(0 > ret)
		{
			perror("write");
			return -1;
		}
		if(0 == strcmp("quit",buf))
		{
			printf("通信结束!\n");
			break;
		}
	}
	
	//关闭socket
	close(sockfd);
}
